package com.andy.customerService.service.impl;


import com.andy.customerService.entity.Role;
import com.andy.customerService.enums.ExceptionEnum;
import com.andy.customerService.exception.BusinessException;
import com.andy.customerService.mapper.RoleMapper;
import com.andy.customerService.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 用户角色表 服务实现类
 * </p>
 *
 * @author Andy
 * @since 2021-12-27
 */
@Service
@Slf4j
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Transactional
    @Override
    public void addRole(Role role) {
        log.info("系统管理-添加角色 入参：{}", role);
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Role::getCode,role.getCode());
        int count = roleMapper.selectCount(lambdaQueryWrapper);
        if (count>0) {
            throw new BusinessException(ExceptionEnum.ROLE_CODE_EXIT);
        }
        roleMapper.insert(role);
    }
}
